/*
 * Copyright 2008 Stuart Moffatt stuartmoffatt@gmail.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.googlecode.emcode.rvha.client.ui;

import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.FormHandler;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormSubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormSubmitEvent;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class Vulnerabilities extends Composite {

    private static Vulnerabilities singleton;
    public static Vulnerabilities get()
    {
        if( singleton == null )
        {
            singleton = new Vulnerabilities();
        }
        return singleton;
    }
    
	private SimplePanel panel;
	private VerticalPanel list;
	
	private CheckBox schools;
	private CheckBox hospitals;
	private CheckBox elderlyCareFacilities;
	private CheckBox myHome;
	private TextBox myHomeTextBox;
	
	
	public Vulnerabilities()
	{
	    panel = new SimplePanel();
	    list = new VerticalPanel();
	    panel.setWidget(list);
	   
	    
	    schools = new CheckBox("Schools");
	    schools.addClickListener(new ClickListener() {
            public void onClick(Widget sender) {
                CheckBox cb = (CheckBox) sender;
                MainLayout.get().toggleKml(MainLayout.KML_SCHOOLS, cb);
            }
	    });
	    schools.setStyleName("rvha-Hazards");
	    schools.setEnabled(true);
	    
	    hospitals = new CheckBox("Hospitals");
	    hospitals.addClickListener(new ClickListener() {
	        public void onClick(Widget sender) {
                CheckBox cb = (CheckBox) sender;
                MainLayout.get().toggleKml(MainLayout.KML_HOSPITALS, cb);
	        }
	    });
	    hospitals.setStyleName("rvha-Hazards");
	    hospitals.setEnabled(true);
	    
	    elderlyCareFacilities = new CheckBox("Elderly Care Facilities");
	    elderlyCareFacilities.addClickListener(new ClickListener() {
	        public void onClick(Widget sender) {
	        	CheckBox cb = (CheckBox) sender;
	        	MainLayout.get().toggleKml(MainLayout.KML_ELDERLY_CARE, cb);
	        }
	    });
	    elderlyCareFacilities.setStyleName("rvha-Hazards");
	    
	    /*
	     * Geocoder
	     */
	    Panel searchPanel = new FlowPanel();
	    final FormPanel form = new FormPanel();
	    form.setAction("#");

	    Panel formElements = new FlowPanel();
	    final TextBox address = new TextBox();
	    address.setVisibleLength(60);
	    address.setText("");
	    address.setWidth("100px");
	    formElements.add(address);

        // add the button
        Button findMyHomeButton = new Button("Go");
        findMyHomeButton.getElement().setAttribute("type", "button");
        findMyHomeButton.addClickListener(new ClickListener(){
            public void onClick(Widget arg0) {
                form.submit();
            }
        });
        formElements.add(findMyHomeButton);
        
	    form.add(formElements);
	    form.addFormHandler(new FormHandler() {
	      public void onSubmit(FormSubmitEvent event) {
	        
              /*
               * Try a more robust way to retrieve both
               * address and hazard information
               */
              // showAddress(address.getText());
              
              /*
               * We do not have PostGIS backing this, so lay
               * off the vulnerability layers
               * getVulnerabilityInformation(address.getText());
               */
              if(!event.isCancelled())
              {
                  event.setCancelled(true);
                  getVulnerabilityInformation(address.getText());
              }
              
              
              
	      }
	      public void onSubmitComplete(FormSubmitCompleteEvent event) {
	      }
	    });
        

        
	    HTML myHome = new HTML("<hr><b>Find my home</b>");
	    myHome.setStyleName("rvha-Hazards");
	    searchPanel.add(myHome);
	    searchPanel.add(form);	    
	    
	    
	    list.add(schools);
	    list.add(hospitals);
	    list.add(elderlyCareFacilities);
	    //list.add(myHome);
	    list.add(searchPanel);
	    
	    
	    initWidget(panel);
	    setStyleName("rvha-Hazards"); // was mail-Tasks
	    
	}
	
	protected void getVulnerabilityInformation(String address) {
        
        VulnerabilityInfoWindowHandler handler = new VulnerabilityInfoWindowHandler();
        
        // this triggers a series of requests
        handler.geoCodeAddress(address);
        
    }
    
    public void getHazardInformation(LatLng coords)
    {
        VulnerabilityInfoWindowHandler handler = new VulnerabilityInfoWindowHandler();
        
        handler.getHazardInformation(coords);
    }

    /*
    private void showAddress(final String address) {
		    //final InfoWindow info = map.getInfoWindow();
		    geocoder.getLocations(address, new LocationCallback() {
		      public void onFailure(int statusCode) {
		        Window.alert("Sorry, we were unable to geocode that address");
		      }

		      public void onSuccess(Placemark[] locations) {
		    	  Placemark place = locations[0];
		    	  String str = "";
		    	  str += place.getAddress();
		    	  
                  //str += " " + place.getStreet();
		    	  //str += " " + place.getCity();
		    	  //str += " " + place.getCounty();
		    	  //str += " " + place.getState();
		    	  //str += " " + place.getCountry();
		    	  //str += " " + place.getPostalCode();
		    	  
		    	  GWT.log("place="+ str, null);
		    	  getZillowInformation(place);
		    	 
		        Placemark place = locations[0];
		        Marker marker = new Marker(place.getPoint());
		        map.addOverlay(marker);
		        String message = place.getAddress() + "<br>" + "<b>Country code:</b> "
		            + place.getCountry();
		        info.open(marker, new InfoWindowContent(message));
		       
		      }
		    });
		  }
          */
	  
	  
	  
}
